package net.zjitc.android2019.ch2activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import net.zjitc.android2019.R;

public class ch2Computer extends AppCompatActivity {
    Button btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn_deng, btn_chu, btn_jia, btn_jian, btn_chen, btn_dian, btn_reture, btn_clear;
    TextView tv_xs;
    EditText tv_srk;
    boolean clr_flag;
    String str;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ch2_computer);

        init();

        View.OnClickListener click = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_ch2_computer_ling:

                    case R.id.btn_ch2_computer_one:

                    case R.id.btn_ch2_computer_two:

                    case R.id.btn_ch2_computer_three:

                    case R.id.btn_ch2_computer_four:

                    case R.id.btn_ch2_computer_five:

                    case R.id.btn_ch2_computer_six:

                    case R.id.btn_ch2_computer_seven:

                    case R.id.btn_ch2_computer_eight:

                    case R.id.btn_ch2_computer_nine:

                    case R.id.btn_ch2_computer_dian:

                        tv_srk.setText(str + ((Button) v).getText());
                        str=tv_srk.getText().toString();
                        break;
                    case R.id.btn_ch2_computer_deng:
                        getResult();
                        break;
                    case R.id.btn_ch2_computer_jia:

                    case R.id.btn_ch2_computer_jian:

                    case R.id.btn_ch2_computer_chen:

                    case R.id.btn_ch2_computer_chu:


                        tv_srk.setText(str+" "+((Button)v).getText()+" ");
                        str=tv_srk.getText().toString();

                        break;
                    case R.id.btn_ch2_computer_reture:
                        if(clr_flag){
                            clr_flag=false;
                            str="";
                            tv_srk.setText("");
                        }
                        else if(str!=null&&!str.equals("")){
                            tv_srk.setText(str.substring(0,str.length()-1));
                        }
                        break;
                    case R.id.btn_ch2_computer_clear:
                        if(clr_flag)
                            clr_flag=false;
                        str="";
                        tv_srk.setText("");
                        break;
                }

            }
        };
        btn0.setOnClickListener(click);
        btn1.setOnClickListener(click);
        btn2.setOnClickListener(click);
        btn3.setOnClickListener(click);
        btn4.setOnClickListener(click);
        btn5.setOnClickListener(click);
        btn6.setOnClickListener(click);
        btn7.setOnClickListener(click);
        btn8.setOnClickListener(click);
        btn9.setOnClickListener(click);
        btn_deng.setOnClickListener(click);
        btn_chu.setOnClickListener(click);
        btn_jia.setOnClickListener(click);
        btn_jian.setOnClickListener(click);
        btn_reture.setOnClickListener(click);
        btn_clear.setOnClickListener(click);
        tv_srk.setOnClickListener(click);
        tv_xs.setOnClickListener(click);


    }

    private void getResult() {
        String exp=tv_srk.getText().toString();
        if(exp==null||exp.equals("")) return ;

        if(!exp.contains(" ")){
            return ;
        }
        if(clr_flag){
            clr_flag=false;
            return;
        }
        tv_xs.setText(exp);
        clr_flag=true;

        String s1=exp.substring(0,exp.indexOf(" "));

        String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);

        String s2=exp.substring(exp.indexOf(" ")+3);
        double cnt=0;
        if(!s1.equals("")&&!s2.equals("")){
            double d1=Double.parseDouble(s1);
            double d2=Double.parseDouble(s2);
            if(op.equals("+")){
                cnt=d1+d2;
            }
            if(op.equals("-")){
                cnt=d1-d2;
            }
            if(op.equals("×")){
                cnt=d1*d2;
            }
            if(op.equals("÷")){
                if(d2==0) cnt=0;
                else cnt=d1/d2;
            }
            if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")) {
                int res = (int) cnt;
                tv_srk.setText(res+"");
            }else {
                tv_srk.setText(cnt+"");}
        }

        else if(!s1.equals("")&&s2.equals("")){
            double d1=Double.parseDouble(s1);
            if(op.equals("+")){
                cnt=d1;
            }
            if(op.equals("-")){
                cnt=d1;
            }
            if(op.equals("×")){
                cnt=0;
            }
            if(op.equals("÷")){
                cnt=0;
            }
            if(!s1.contains(".")) {
                int res = (int) cnt;
                tv_srk.setText(res+"");
            }else {
                tv_srk.setText(cnt+"");}
        }

        else if(s1.equals("")&&!s2.equals("")){
            double d2=Double.parseDouble(s2);
            if(op.equals("+")){
                cnt=d2;
            }
            if(op.equals("-")){
                cnt=0-d2;
            }
            if(op.equals("×")){
                cnt=0;
            }
            if(op.equals("÷")){
                cnt=0;
            }
            if(!s2.contains(".")) {
                int res = (int) cnt;
                tv_srk.setText(res+"");
            }else {
                tv_srk.setText(cnt+"");}
        }
        else {
            tv_srk.setText("");
        }
    }


    private void init() {
        btn0 = findViewById(R.id.btn_ch2_computer_ling);
        btn1 = findViewById(R.id.btn_ch2_computer_one);
        btn3 = findViewById(R.id.btn_ch2_computer_three);
        btn4 = findViewById(R.id.btn_ch2_computer_four);
        btn5 = findViewById(R.id.btn_ch2_computer_five);
        btn6 = findViewById(R.id.btn_ch2_computer_six);
        btn7 = findViewById(R.id.btn_ch2_computer_seven);
        btn8 = findViewById(R.id.btn_ch2_computer_eight);
        btn9 = findViewById(R.id.btn_ch2_computer_nine);
        btn_deng = findViewById(R.id.btn_ch2_computer_deng);
        btn_chu = findViewById(R.id.btn_ch2_computer_chu);
        btn_jia = findViewById(R.id.btn_ch2_computer_jia);
        btn2 = findViewById(R.id.btn_ch2_computer_two);
        btn_jian = findViewById(R.id.btn_ch2_computer_jian);
        btn_chen = findViewById(R.id.btn_ch2_computer_chen);
        btn_dian = findViewById(R.id.btn_ch2_computer_dian);
        btn_reture = findViewById(R.id.btn_ch2_computer_reture);
        btn_clear = findViewById(R.id.btn_ch2_computer_clear);
        tv_srk = findViewById(R.id.ch2_computer_srk);
        tv_xs = findViewById(R.id.ch2_tv_computer_xs);
    }
}
